﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace igflow
{
    class NodeBoxMesh : Mesh
    {
        internal float cornerW = 0.25f;
        internal short indCount = 0;
        internal short vCount = 0;
        public NodeBoxMesh()
        {

        }
        public NodeBoxMesh(float w, float h, string texture)
        {
            //cornerW = w / 4;
            if (w < (cornerW * 3))
            {
                w = cornerW * 3;
            }
            if (h < (cornerW * 3))
            {
                h = cornerW * 3;
            }
            float newW = w - (2 * cornerW);
            float newh = w - (2 * cornerW);

            Textures = new List<Texture2D>();
            Textures.Add(World.contentManager.Load<Texture2D>(texture));
            vertexDeclaration = new VertexDeclaration(World.Graphics.GraphicsDevice, VertexPositionColorTexture.VertexElements);

            Vertices = new VertexPositionColorTexture[4 * 16];
            Indexes = new short[6 * 16];

            Vector3 TL = new Vector3(-w / 2, h / 2, 0.0f);
            CreateQuad(TL, cornerW,cornerW, new Vector2(0,0), new Vector2(0.25f,0.25f));
            TL += new Vector3(cornerW, 0, 0);
            CreateQuad(TL, newW, cornerW, new Vector2(0.25f,0), new Vector2(0.75f,0.25f) );
            TL += new Vector3(newW, 0, 0);
            CreateQuad(TL, cornerW, cornerW, new Vector2(0.75f, 0), new Vector2(1.0f, 0.25f));

            // Main bit along the middle
            TL = new Vector3(-w / 2, h / 2, 0.0f);
            TL += new Vector3(0, -cornerW, 0);
            CreateQuad(TL, cornerW, newh, new Vector2(0, 0.25f), new Vector2(0.25f, 0.75f));
            TL += new Vector3(cornerW, 0, 0);
            CreateQuad(TL, newW, newh, new Vector2(0.25f, 0.25f), new Vector2(0.75f, 0.75f));
            TL += new Vector3(newW, 0, 0);
            CreateQuad(TL, cornerW, newh, new Vector2(0.75f, 0.25f), new Vector2(1.0f, 0.75f));


            TL = new Vector3(-w / 2, h / 2, 0.0f);
            TL += new Vector3(0, -cornerW - newh, 0);
            CreateQuad(TL, cornerW, cornerW, new Vector2(0, 0.75f), new Vector2(0.25f, 1.0f));
            TL += new Vector3(cornerW, 0, 0);
            CreateQuad(TL, newW, cornerW, new Vector2(0.25f, 0.75f), new Vector2(0.5f, 1.0f));
            TL += new Vector3(newW, 0, 0);
            CreateQuad(TL, cornerW, cornerW, new Vector2(0.75f, 0.75f), new Vector2(1.0f, 1.0f));

        }

        public void CreateQuad(Vector3 start, float w, float h, Vector2 tl, Vector2 br)
        {
            // Fill in texture coordinates to display full texture
            // on quad
            float uw = br.X - tl.X;
            float uh = br.Y - tl.Y;

            Vector2 textureUpperLeft = tl;
            Vector2 textureUpperRight = new Vector2(tl.X + uw, tl.Y);
            Vector2 textureLowerLeft = new Vector2(tl.X, tl.Y + uh);
            Vector2 textureLowerRight = br;

            // Set the position and texture coordinate for each
            // vertex


            Vertices[vCount + 0].Position = new Vector3(start.X, start.Y , start.Z);
            Vertices[vCount + 0].TextureCoordinate = textureUpperLeft;
            Vertices[vCount + 0].Color = Color.White;

            Vertices[vCount + 1].Position = new Vector3(start.X + w, start.Y, start.Z);
            Vertices[vCount + 1].TextureCoordinate = textureUpperRight;
            Vertices[vCount + 1].Color = Color.White;

            Vertices[vCount + 2].Position = new Vector3(start.X,start.Y - h, start.Z);
            Vertices[vCount + 2].TextureCoordinate = textureLowerLeft;
            Vertices[vCount + 2].Color = Color.White;

            Vertices[vCount + 3].Position = new Vector3(start.X + w, start.Y - h, start.Z);
            Vertices[vCount + 3].TextureCoordinate = textureLowerRight;
            Vertices[vCount + 3].Color = Color.White;

            // Set the index buffer for each vertex, using
            // clockwise winding

            Indexes[indCount + 0] = (short)(vCount + 0);
            Indexes[indCount + 1] = (short)(vCount + 1);
            Indexes[indCount + 2] = (short)(vCount + 2);
            Indexes[indCount + 3] = (short)(vCount + 2);
            Indexes[indCount + 4] = (short)(vCount + 1);
            Indexes[indCount + 5] = (short)(vCount + 3);

            vCount += 4;
            indCount += 6;

        }
    }
}
